<?php
defined('SYSPATH') OR die('No direct access allowed.');
/**
 * MAPI COA Project
 * mapi.php
 * @author ranuandra
 *
 * 22 Jul 2011
 * tags
 */

abstract class Kohana_Mapi extends Controller_Template

{
	
	public $main_template = '';
	
	public $template = 'main';
      // Controls access for the whole controller, if not set to FALSE we will only allow user roles specified
      // Can be set to a string or an array, for example array('login', 'admin') or 'login'
    public $auth_required = FALSE;
 
      // Controls access for separate actions
      // 'adminpanel' => 'admin' will only allow users with the role admin to access action_adminpanel
      // 'moderatorpanel' => array('login', 'moderator') will only allow users with the roles login and moderator to access action_moderatorpanel
    public $secure_actions = FALSE;
	
/**
      * Initialize properties before running the controller methods (actions),
      * so they are available to our action.
      */
/**
      * Initialize properties before running the controller methods (actions),
      * so they are available to our action.
      */
    
     public function before()
      {
         // Run anything that need ot run before this.
         //$is_mobile = Request::user_agent('mobile');
         //print_r(Request::detect_uri());
    	 //if($is_mobile)
    	 //{
    	//	$this->main_template = "mobile"; 
    	 //}
    	 //else
    	 //{
    	//	$this->main_template = "web";
    	 //};
    	 //$this->template = "main";
         parent::before();
         
         //echo Request::current()->controller();
         $action_name = Request::current()->action();
         //echo $action_name;
         if (($this->auth_required !== FALSE && Auth_ORM::instance()->logged_in($this->auth_required) === FALSE) || (is_array($this->secure_actions) && array_key_exists($action_name, $this->secure_actions)))
		 {
			if (Auth_ORM::instance()->logged_in()){
				
				Request::current()->redirect(URL::base().'productspec');
				
				/**if(Member::instance()->get_user()->activated == 0)
				{
					Request::instance()->redirect(URL::base().'register/activated/'.Member::instance()->get_user()->id);

				}
				else
				{
					if(Member::instance()->get_user()->verified == 0)
					{
						Request::instance()->redirect(URL::base().'member/detail');
					}					
				}**/
			}else{
				Request::current()->redirect(URL::base());
			}
		 }
		 
		 
         if($this->auto_render)
          {
            // Initialize empty values
            $this->template->title            = '';
            $this->template->meta_keywords    = '';
            $this->template->meta_description = '';
            $this->template->meta_copywrite   = '';
            $this->template->header           = '';
            $this->template->content          = '';
            $this->template->footer           = '';
            $this->template->styles           = array();
            $this->template->scripts          = array();
            $this->template->main			  = '';
          }
      }

     /**
      * Fill in default values for our properties before rendering the output.
      */
     public function after()
      {
         if($this->auto_render)
          {
             // Define defaults
             $styles                  = array(URL::base().'assets/css/reset.css' => 'screen');
             $scripts                 = array(URL::base().'assets/js/jquery-1.4.2.min.js');

             // Add defaults to template variables.
             $this->template->styles  = array_reverse(array_merge($this->template->styles, $styles));
             $this->template->scripts = array_reverse(array_merge($this->template->scripts, $scripts));
             $this->template->ref = "";
            
           }

         // Run anything that needs to run after this.
         parent::after();
      }   

}
?>